<?php
/**
 * @title 1466. 重新规划路线
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer $n
     * @param Integer[][] $connections
     * @return Integer
     */
    function minReorder($n, $connections) {
        /**
         * @since 2023-12-07 记录方向
         */
        $directionArray = [];

        /**
         * @since 2023-12-07 建立点到点的路径
         */
        $pointToPointArray = [];
        foreach ($connections as list($s, $e)){
            /**
             * @since 2023-12-07 记录方向
             */
            $directionArray[$s][$e] = 0;

            /**
             * @since 2023-12-07 点和点之间有路
             */
            $pointToPointArray[$s][$e] = $e;
            $pointToPointArray[$e][$s] = $s;
        }

        /**
         * @since 2023-12-07 方向数量
         */
        $directionCnt = 0;
        
        /**
         * @since 2023-12-07 以0为根节点，层次遍历
         */
        $nodeArray = [0];
        while($nodeArray){
            /**
             * @since 2023-12-07 下次遍历层次点
             */
            $nextNodeArray = [];

            /**
             * @since 2023-12-07 遍历当前层点
             */
            foreach ($nodeArray as $point){
                /**
                 * @since 2023-12-07 遍历下一层节点
                 */
                foreach ($pointToPointArray[$point] as $nextPoint){
                    /**
                     * @since 2023-12-07 需要修改的方向
                     */
                    if(isset($directionArray[$point][$nextPoint])){
                        $directionCnt++;
                    } else {}

                    /**
                     * @since 2023-12-07 下次遍历的点
                     */
                    $nextNodeArray[] = $nextPoint;

                    /**
                     * @since 2023-12-07 避免回溯
                     */
                    unset($pointToPointArray[$nextPoint][$point]);
                }
            }

            /**
             * @since 2023-12-07 赋值下次遍历层次点
             */
            $nodeArray = $nextNodeArray;
        }

        /**
         * @return
         */
        return $directionCnt;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202312/2023.12.07.php
 */
$datas = [
    [6, [[0,1],[1,3],[2,3],[4,0],[4,5]]], // 3
    [5, [[1,0],[1,2],[3,2],[3,4]]], // 2
    [3, [[1,0],[2,0]]], // 0
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->minReorder(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
